from flask import Blueprint, session, render_template, redirect, request

from models import Post, Comment, Prove, unpass, User, insert

check_blueprint=Blueprint('check', __name__)

def can_check(uid):
    return uid == 1

@check_blueprint.route('/check')
def check():
    uid = session.get('uid')
    root = can_check(uid)
    if root:
        posts = Post.get_by_status(False)
        comments = Comment.get_by_status(False)
        proves = Prove.get_by_status(False)
        return render_template('check/index.html',root=root,posts=posts,comments=comments,proves=proves)
    return render_template('check/index.html',root=root)

def back():
    return redirect(request.referrer)

@check_blueprint.route('/check/unpass/prove/<int:id>')
def check_unpass_prove(id):
    p=Prove.query.filter_by(id=id).first()
    unpass(p)
    return back()

@check_blueprint.route('/check/unpass/post/<int:id>')
def check_unpass_post(id):
    p=Post.query.filter_by(id=id).first()
    unpass(p)
    return back()

@check_blueprint.route('/check/unpass/comment/<int:id>')
def check_unpass_comment(id):
    p=Comment.query.filter_by(id=id).first()
    unpass(p)
    return back()

#done
@check_blueprint.route('/check/pass/prove/<int:id>')
def check_pass_prove(id):
    p=Prove.query.filter_by(id=id).first()
    unpass(p,False)
    u = User.query.filter_by(id=p.uid).first()
    u.role=p.role
    insert(u)
    return back()

@check_blueprint.route('/check/pass/post/<int:id>')
def check_pass_post(id):
    p=Post.query.filter_by(id=id).first()
    unpass(p,False)
    return back()

@check_blueprint.route('/check/pass/comment/<int:id>')
def check_pass_comment(id):
    p=Comment.query.filter_by(id=id).first()
    unpass(p,False)
    return back()
